<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-9
 * Time: 16:56
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\Nav as NavModel;
use think\Db;

class Nav extends Admin
{
    //轮播图列表
    public function navlist()
    {
        // 读取数据
        $data_list = Db::name('qt_nav')->order('sort asc,id asc')->select();
        //查询属性组
        $cats =NavModel::all()
            ->order('sort desc')
            ->column('name', 'id');
        $cats[0] = '顶级栏目';
        $btn_access1 = [
            'title' => '修改导航',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access = [
            'title' => '新增导航',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $btn_access2 = [
            'title' => '删除导航',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default',
            'href' => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('name', '导航名称', 'text')
            ->addColumn('logo', '导航图片', 'picture')
            ->addColumn('pid', '上级分类', 'status', '', $cats)
            ->addColumn('sort', '排序', 'text.edit')
            ->addColumn('is_index', '首页展示', 'status','',['0'=>'不展示:info','1'=>'展示:success'])
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('qt_nav') // 指定数据表名
            ->addTopButton('custom', $btn_access) // 添加顶部按钮
            ->addRightButton('custom', $btn_access1) // 添加右侧按钮
            ->addRightButton('custom', $btn_access2) // 添加右侧按钮
            ->setPrimaryKey('id') // 设置主键名为username
            ->setPageTitle('导航列表')
            ->setRowList($data_list) // 设置表格数据
            ->fetch();
    }

    //新增导航
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            Db::name('qt_nav')->insert($data);
            $this->success('添加成功', 'navlist');

        } else {
            //查询属性组
            $cats =NavModel::where('pid', 0)
                ->order('sort desc')
                ->column('name', 'id');
            $cats['0'] = '顶级分类';
            ksort($cats);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '导航名称'],
                        ['image:7', 'logo', '导航图片'],
                        ['text:7', 'sort', '排序', '越小越靠前</code>', '99'],
                        ['select', 'pid', '上级分类', '', $cats],
                        ['radio', 'is_index', '首页展示', '', ['0'=>'不展示','1'=>'展示'],'0'],
                    ]
                )
                ->setPageTitle('添加导航')
                ->fetch();
        }
    }

    //删除导航
    public function delete($id = null)
    {
        Db::name('qt_nav')->where('id', $id)->delete();
        $this->success('删除成功');
    }

    //修改导航
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            Db::name('qt_nav')->where('id', $id)->update($data);
            $this->success('修改成功', 'navlist');
        } else {
            $info = Db::name('qt_nav')->where('id', $id)->find();
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '导航名称'],
                        ['image:7', 'logo', '导航图片'],
                        ['text:7', 'sort', '排序', '越小越靠前</code>', '99'],
                        ['radio:7', 'is_index', '首页展示', '',['0'=>'不展示','1'=>'展示']],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('修改导航信息')
                ->fetch();
        }
    }

}